package cn.tedu.v2_1;

import java.io.*;
import java.net.ConnectException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

/**
 * Version  2.1  多人聊天室
 * 知识点：多线程并发
 * 实现步骤：1.Server类---创建多线程CS  ：  目的：  处理多个客户端发来的链接
 *         1.1找到Server类--主方法：加入内部类--多线程CS
 *         2.Server类--读入操作，存入到线程当中
 */


public class Client {
    private Socket socket;

    public Client() {
        try{
            socket=new Socket("127.0.0.1",7878);
        }catch (NullPointerException e){
            System.out.println("友情提示：IP传入出错导致连接失败");
        }catch (UnknownHostException e){
            System.out.println("友情提示：IP传入错误");
        }catch(ConnectException e){
            System.out.println("客户端链接服务器端失败，提示：检查IP/端口");
        }catch(IOException e){
            System.out.println("客户端链接服务器端失败，提示：检查IP/端口");
        }
    }
    public void start()  {

        PrintWriter pw=null;
        try{


         OutputStream os =socket.getOutputStream();
         InputStream is=socket.getInputStream();
         OutputStreamWriter osw=new OutputStreamWriter(os,"UTF8");
         InputStreamReader isr=new InputStreamReader(is);
         BufferedWriter bw=new BufferedWriter(osw);
         pw=new PrintWriter(bw,true);
         System.out.println("请对李佳怡说话：");
         Scanner input=new Scanner(System.in);
        String line=null;
        while(true) {
            line = input.nextLine();
            pw.println(line);
            System.out.println("好的");
        }
        } catch(UnknownHostException e){
                System.out.println("客户端/服务器编码集有问题");
        } catch(IOException e){
                System.out.println("客户端/服务器编码集有问题");
            } finally{
        if (pw!=null)
            pw.close();
        }
    }


    public static void main(String[] args){
        Client client=new Client();
        client.start();
    }



}